﻿using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using System.Transactions;
using ChuanYe.CountyFireManagement.Model;

namespace ChuanYe.CountyFireManagement.BLL
{
    public class MessageBLL
    {
        #region 构造单例
        private MessageBLL() { }
        private static MessageBLL _instance;
        public static MessageBLL Instance
        {
            get
            {
                return _instance ?? (_instance = new MessageBLL());
            }
        }
        #endregion


        public List<Message> GetMessage(int messageId,int fireId,string addressCounty, 
                                        string addressTown, string addressVillage, int? isRead, List<int> messageTypes,
                                        List<int> messageSendTypes,int targetUserId)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var fireLinq = from a in ef.Fireinfo select a;

                if (!string.IsNullOrEmpty(addressCounty))
                {
                    fireLinq = fireLinq.Where(p => p.AddressCounty == addressCounty);
                }
                if (!string.IsNullOrEmpty(addressTown))
                {
                    fireLinq = fireLinq.Where(p => p.AddressTown == addressTown);
                }
                if (!string.IsNullOrEmpty(addressVillage))
                {
                    fireLinq = fireLinq.Where(p => p.AddressVillage == addressVillage);
                }
                List<int> fireIDs = fireLinq.Select(s => s.FireId).ToList();

                var messgeLinq = from a in ef.Message select a;
                if (messageId > 0)
                {
                    messgeLinq = messgeLinq.Where(p => p.MessageId== messageId);
                }
                if (fireId > 0)
                {
                    messgeLinq = messgeLinq.Where(p => p.FireId == fireId);
                }

                if (fireIDs != null && fireIDs.Count > 0)
                {
                    messgeLinq = messgeLinq.Where(p => fireIDs.Contains(p.FireId));
                }

                if (messageTypes != null && messageTypes.Count > 0)
                {
                    messgeLinq = messgeLinq.Where(p => messageTypes.Contains(p.Type));
                }
                if (isRead.HasValue)
                {
                    messgeLinq = messgeLinq.Where(p => p.IsRead == isRead);
                }

                if (messageSendTypes!=null && messageSendTypes.Count > 0)
                {
                    messgeLinq = messgeLinq.Where(p => messageSendTypes.Contains(p.SendType));
                }
                var result = messgeLinq.ToList();

                List<Message> orList = new List<Message>();
                if (targetUserId > 0)
                {
                    var orLinq = messgeLinq.Where(p=>p.TargetUserId==p.TargetUserId);
                    orList = orLinq.ToList();
                }


                foreach (var item in orList)
                {
                    var exist = result.Where(p => p.MessageId == item.MessageId).FirstOrDefault();
                    if (exist == null)
                    {
                        result.Add(item);
                    }
                }
                return result;
            }
        }


        /// <summary>
        /// 设置火灾 已读状态
        /// </summary>
        /// <param name="fireId"></param>
        /// <param name="messageTypes">参考枚举类型 MessageType</param>
        /// <param name="isRead">0.未读 1.已读</param>
        /// <returns></returns>
        public int ChangeMessageReadStatus(int fireId, List<int> messageTypes,short? isRead) 
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var queryLinq = from a in ef.Message select a;
                if (fireId > 0)
                {
                    queryLinq=queryLinq.Where(p => p.FireId == fireId);
                }
                if (messageTypes != null && messageTypes.Count > 0)
                {
                    queryLinq=queryLinq.Where(p => messageTypes.Contains(p.Type));
                }
                var list = queryLinq.ToList();
                foreach (var item in list)
                {
                    item.IsRead = isRead;
                    ef.Message.Add(item);
                    ef.Entry<Message>(item).State = EntityState.Modified;
                    ef.SaveChanges();
                }
            }
            return 1;
        }



        public int ChangeMessageReadStatus(int messageId, short? isRead)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var queryLinq = from a in ef.Message select a;
                if (messageId > 0)
                {
                    queryLinq = queryLinq.Where(p => p.MessageId == messageId);
                }
                var list = queryLinq.ToList();
                foreach (var item in list)
                {
                    item.IsRead = isRead;
                    ef.Message.Add(item);
                    ef.Entry<Message>(item).State = EntityState.Modified;
                    ef.SaveChanges();
                }
            }
            return 1;
        }





        public int Add(Message info,List<int> ignoreMessageTypes)
        {
            using (CountyFireManagementContext ef = new CountyFireManagementContext())
            {
                var linq = from a in ef.Message select a;
                linq = linq.Where(p => p.FireId == info.FireId && p.Type == info.Type);
                if (ignoreMessageTypes != null && ignoreMessageTypes.Count > 0)
                {
                    linq = linq.Where(p => !ignoreMessageTypes.Contains(p.Type));
                }
                var exist = linq.FirstOrDefault();
                if (exist != null)
                {
                    return 0;
                }

                ef.Message.Add(info);
                ef.Entry<Message>(info).State = EntityState.Added;
                ef.SaveChanges();
                return info.MessageId;
            }
        }



        /// <summary>
        ///  将Message转换成MQTT数据
        /// </summary>
        /// <param name="messages"></param>
        /// <param name="recordType"></param>
        /// <returns></returns>
        public List<FireMQTTResponseModel> TransMessageToFireMQTTResponseModel(List<Message> messages,int recordType)
        {
            List<FireMQTTResponseModel> list = new List<FireMQTTResponseModel>();
            foreach (var item in messages)
            {
                FireMQTTResponseModel fmm = new FireMQTTResponseModel();
                fmm.FireId = item.FireId;
                fmm.MessageId = item.MessageId;
                fmm.MessageContent = item.Content;
                fmm.MessageType = item.Type;
                fmm.RecordType = recordType;
                fmm.RecordContent = item.Content;
                fmm.MessageSendType = item.SendType;
                fmm.SendUserId = item.SendUserId;
                fmm.TargetUserId = item.TargetUserId;
                list.Add(fmm);
            }
            return list;
        }

        /// <summary>
        /// 扑救力量MQTT   特殊处理:向区域发送
        /// </summary>
        /// <param name="messages"></param>
        /// <param name="addressCounty"></param>
        /// <param name="addressVillage"></param>
        /// <param name="addressTown"></param>
        /// <param name="recordType"></param>
        /// <returns></returns>

        public FireMQTTResponseModel TransMessageToFireFightingForceMQTTResponseModel(Message message, string addressCounty, string addressTown, string addressVillage, int recordType)
        {
            FireMQTTResponseModel fmm = new FireMQTTResponseModel();
            fmm.FireId = message.FireId;
            fmm.MessageId = message.MessageId;
            fmm.MessageContent = message.Content;
            fmm.MessageType = message.Type;
            fmm.RecordType = recordType;
            fmm.RecordContent = message.Content;
            fmm.MessageSendType = message.SendType;
            fmm.SendUserId = message.SendUserId;
            fmm.TargetUserId = message.TargetUserId;
            fmm.AddressCounty = addressCounty;
            fmm.AddressTown = addressTown;
            fmm.AddressVillage = addressVillage;
            return fmm;
        }



    }
}
